package com.eryos.test.springsecurity.server.login;

/**
 * Project :  SpringSecuritySimple
 * Author :   Olivier Combe
 * URL :      http://eryos-tech.blogspot.com/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

import org.springframework.security.authentication.encoding.ShaPasswordEncoder;

import com.eryos.test.springsecurity.login.service.LoginService;
import com.eryos.test.springsecurity.shared.SimpleUser;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class LoginServiceImpl extends RemoteServiceServlet implements
LoginService {

	private static final String USER_TYPE 			= "SimpleUser";
	private static final String USER_USERNAME  		= "username"; 
	private static final String USER_ENABLED  		= "enabled";
	private static final String USER_AUTHORITIES 	= "roles";
	private static final String USER_SALT  			= "salt"; 
	private static final String USER_ENCODED_PASS 	= "sha_password"; 

	/**
	 * 
	 */
	public boolean registerNewUser(SimpleUser usr){		
		try {

			/* Register user in DataStore */
			registerUser(usr);

			/* Send mail for activation ? */
			// TODO

			/* Do some usefull stuff with the new User ? */
			// TODO 

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	/**
	 * Insert a new SimpleUser object in the DataStore
	 * @param usr
	 */
	private void registerUser(SimpleUser usr) { 

		Key key = KeyFactory.createKey(USER_TYPE, usr.getUsername()); 
		Entity user = new Entity(key); 

		ShaPasswordEncoder pass = new ShaPasswordEncoder();
		String encodedPassword = pass.encodePassword(usr.getPassword(), usr.getUsername());

		user.setProperty(USER_USERNAME, usr.getUsername()); 
		user.setProperty(USER_ENCODED_PASS, encodedPassword); 
		user.setProperty(USER_SALT, usr.getUsername()); 
		user.setUnindexedProperty(USER_ENABLED, true/*usr.isEnabled()*/); // by default, the user is disabled (cf. SimpleUser constructor)

		user.setUnindexedProperty(USER_AUTHORITIES, usr.getAuthorities()); 

		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService(); 
		datastore.put(user);    
	}

}
